import numpy as np
import matplotlib.pyplot as plt
from rk45 import cal_temperature
import time
from data_record import data_record
R_1 = 0.0012
R_2 = 0.0093
C_in = 1100000
C_wall = 186000000
theta_out = -20
dt = 10


cal_temp = []
dr = []
# init
for i in range(6):
    cal_temp.append(cal_temperature(dt, R_1, R_2, C_in, C_wall))
    dr.append(data_record(i,'test'))
    if(i%2 == 0):
        cal_temp[i].reset_B_P()
    else:
        cal_temp[i].reset_B_0()

    cal_temp[i].reset_out_tempurature(theta_out)
    cal_temp[i].set_start_theta(np.random.uniform(18, 22), 18)

# cal
last_time = time.time()
for i in range(6):
    print("cal %ith"%i)
    for index in range(int(86400/dt)):
        cal_temp[i].rk45()
        if (cal_temp[i].get_theta()[0,0] < 18):
            cal_temp[i].reset_B_P()
        #     print("less than 18,now temp %d"%cal_temp.get_theta()[0,0])
        
        elif (cal_temp[i].get_theta()[0,0] > 22):
            cal_temp[i].reset_B_0()
        #     print("over 22,now temp %d"%cal_temp.get_theta()[0,0])
        dr[i].data_append(index,cal_temp[i].get_theta()[0,0],cal_temp[i].get_theta()[1,0],cal_temp[i].P_status)
print("cost time {}s".format(time.time() - last_time))


# read data and plot
for i in range(6):
    dr[i].data_dump()

    plot_data = dr[i].data_get()
    x_axis = plot_data[:,0]/360
    temp_in = plot_data[:,1]
    temp_wall = plot_data[:,2]
    P_status = plot_data[:,3]
    plt.plot(x_axis,temp_in)
    plt.plot(x_axis,temp_wall)
plt.show()

